package flex.remoteObjectTest;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ordobill.flex.common.FiguresTable;
import com.ordobill.flex.common.ModelingTable;
import com.ordobill.webapp.action.ProjectAction;
import com.ordobill.webapp.beans.Figures;
import com.ordobill.webapp.beans.Modeling;
import com.ordobill.webapp.beans.SimulationFields;
import com.ordobill.webapp.beans.SystemImg;
import com.ordobill.webapp.common.SqlMapClientManager;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;


public class RemoteJava {
    
    private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
    private SqlMapClient sqlMap = SqlMapClientManager.getSqlMapClient();
    
    private MessageBroker msgBroker=MessageBroker.getMessageBroker(null);
    private String clientID = UUIDUtils.createUUID(false);
    
    public RemoteJava(){
        System.out.println("RemoteJava");
    }
    
    
    //로드데이터를 플렉스로 보내주는 메서
    public Modeling[] sendLoadData(String prdUid) throws Exception{
    	
    	return ModelingTable.modelingList(new Modeling(prdUid));
    }
    
    public Item getItem(){
        Item subItem1 = new Item();
            subItem1.setName("subITem1");
//          subItem1.setSubItems(null);
        Item subItem2 = new Item();
            subItem2.setName("subItem2");
//          subItem2.setSubItems(null);
        Item branchItem = new Item();
            branchItem.setName("branchItem");
            Item subItems[] = {subItem2,subItem2};
            branchItem.setSubItems(subItems);
        return branchItem;
    }
    
    public int setModeling(Modeling[] arr) throws Exception{
        System.out.println("setItem : "+arr);
        int message = -1;
        try{
            sqlMap.startTransaction();
            message = ModelingTable.modelingPreDelete(sqlMap, arr);
            
            if(message < 0){
                return message;
            }
            
            message = ModelingTable.modelingInsert(sqlMap,arr);
            
            if(message < 0){
                return message;
            }
            
            sqlMap.commitTransaction();
            
        }catch(Exception e){
            System.out.println(e.getMessage());
            log.debug(e.getMessage());
        }finally{
            sqlMap.endTransaction();
        }
        
        return message;
        
    }
    
    public int setFigures(Figures[] arr) throws Exception{
        log.debug("FIgures count is "+arr.length);
        int message = -1;
        try{
            sqlMap.startTransaction();
            message = FiguresTable.figuresDelete(sqlMap, arr);
            
            if(message < 0){
                return message;
            }
            
            message = FiguresTable.figuresInsert(sqlMap, arr);
            
            if(message < 0){
                return message;
            }
            
            sqlMap.commitTransaction();
        }catch(Exception e){
            System.out.println(e.getMessage());
            log.debug(e.getMessage());            
        }finally{
            sqlMap.endTransaction();
        }
        
        return message;
    }
    
    public int deleteFigure(int figUid) throws Exception {
       int message = -1;
       
       try{
           sqlMap.startTransaction();
           message =  FiguresTable.figursDelete(sqlMap, figUid);
           
           if(message < 0){
               return message;
           }
           
           sqlMap.commitTransaction();
       }catch(Exception e){
           System.out.println(e.getMessage());
           log.debug(e.getMessage()); 
       }finally{
           sqlMap.endTransaction();
       }
       
       return message;
    }
    
    public Figures[] getFigures(int projectUid, String figEqUid) throws Exception {
        return FiguresTable.figuresList(projectUid, figEqUid);
    }
    
    public SimulationFields[] getSimulationFigures(String sessionId){
        log.debug("Parameter Session Id is :"+sessionId);
        SimulationFields[] simFields = ProjectAction.simulationArr.get(sessionId);
        ProjectAction.simulationArr.remove(sessionId);
        return simFields;
    }
	
	@SuppressWarnings("unchecked")
    public String[] sendIconImgToFlex() throws Exception{
		ArrayList<SystemImg> imgList = (ArrayList<SystemImg>) sqlMap.queryForList("SystemImg.list", new SystemImg());
		String[] imgName = new String[imgList.size()];
		int _idx = 0;
		for(SystemImg img : imgList){
		    imgName[_idx] = img.getSysimgFile();
		    _idx++;
		}
		
		return imgName;
	}
	
	
}

